package com.wisdom.client.handler;

import com.wisdom.core.data.RpcResponse;
import com.wisdom.core.exception.RpcException;
import com.wisdom.core.netty.request.RequestPromise;
import com.wisdom.core.netty.request.RpcRequestHolder;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @description: <将RpcResponse对象处理为业务逻辑数据并返回>
 * @author: Leo
 * @date: 2021-08-23 19:35
 */
@Slf4j
@ChannelHandler.Sharable
public class RpcResponseHandler extends SimpleChannelInboundHandler<RpcResponse> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcResponse response) {
        try {
            //异步转同步操作
            RequestPromise promise = RpcRequestHolder.getRequestPromise(response.getRequestId());
            if (promise != null){
                promise.setSuccess(response);
            }
        }catch (Exception e){
            log.error("RpcResponseHandler channelRead0 error,msg is {}", e.getMessage() );
            throw new RpcException(e.getMessage(),e);
        }
    }
}
